<?php

namespace EasyWaf;

class Utils
{
    /**
     * isCli
     * @return bool
     */
    public static function isCli()
    {
        if (php_sapi_name() === 'cli') {
            return true;
        }
        return false;
    }

    /**
     * match
     * @param $data
     * @param $filter
     * @return string
     */
    public static function match($data, $filter)
    {
        $char = '';
        foreach ($data as $key => $value) {
            if (preg_match("/" . $filter . "/is", $key, $res) == 1) {
                $char = $res['0'];
                break;
            }
            $value = self::objectToString($value);
            if (preg_match("/" . $filter . "/is", $value, $res) == 1) {
                $char = $res['0'];
                break;
            }
        }
        return $char;
    }

    /**
     * objectToString
     * @param $object
     * @return mixed|string
     */
    public static function objectToString($object)
    {
        static $result;
        static $keyText;
        if (!is_array($object)) {
            return $object;
        }
        foreach ($object as $key => $val) {
            $keyText = $keyText . $key;
            if (is_array($val)) {
                self::objectToString($val);
            } else {
                $result[] = $val . $keyText;
            }
        }
        return implode($result);
    }
}